<?php 
    session_start();
    if(!isset($_SESSION['islogined'])||@$_SESSION['idposition']==3)
        header("location:./Login.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link href="app.css" rel="stylesheet">
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

</head>
<body>
    <?php include 'header1.php'; ?>
    <div class="container">
        <ol class="breadcrumb path-nav">
            <li><a href="./Home.php">Home</a></li>
            <li class="active">Template Management</li>
        </ol>
    </div>
    <div class="container">
        <div class="col-md-2">
            <button id="creat_tpl" class="btn btn-default ">
            <span class="glyphicon glyphicon-plus"> </span>
            Create New Template</button>
            
        </div>
        <div class="col-md-3 col-md-offset-7 ">
            <div class="input-group">
                <input id='serch_box' type="text" class="form-control" placeholder="Template Name">       
                <span id='serch_button' class="glyphicon glyphicon-search input-group-addon"></span>         
            </div>                     
        </div>            
    </div>
    <div id="showall" class="list-container">
        <div class="container template-list-item template-list-header">
            <div class="col-md-5">Template Name</div>       
            <div class="col-md-2 ">View</div>
            <div class="col-md-2 ">Update</div>
            <div class="col-md-2 ">Delete</div>
        </div>
        <div id='show_message'>
        <?php
            include'infor_template.php';
            showsuperlist();
        ?>
        </div>
        
    </div> 
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
</div>
<script src="jquery-3.0.0.min.js"></script>
<script src="bootstrap.min.js"></script>
<script>
    $(function(){
        bottomclc();
        buttonclc();
        $("#serch_button").click(function(){
            var name=$("#serch_box").val();
            $.post("infor_template.php",{
                serch_name:name
            },function(data){
                $("#showall").html(data);
                bottomclc();
                buttonclc();
            });
        });
        $("#creat_tpl").click(function(){
            $(window).attr('location','CreateTemplate.php');
        });
        $(document).on('click','#delete_tpl',function(){
            $(".success-modify").remove();
            var tplid=$(this).data('id');
            $.post('infor_template.php',{
                delete_tpl:1,
                tplid:tplid
            },function(data){
                if(data=='success'){
                    $(".l_"+tplid).hide();
                    $('.template-list-header').before("<div class='success-modify'>The template deleted successfully</div>");
                }
            });
        });
    });
    function bottomclc(){
        $(".subtemplete").unbind('click').bind('click',function(){
            if($(this).hasClass('glyphicon-triangle-bottom')){
                $(this).removeClass('glyphicon-triangle-bottom');
                $(this).addClass('glyphicon-triangle-left');
            }
            else{
                $(this).removeClass('glyphicon-triangle-left');
                $(this).addClass('glyphicon-triangle-bottom');
            }
            var id=$(this).data('id');
            $("."+id).toggle();
        });
    }
    function buttonclc(){
        $(".btn-sm").unbind('click').bind('click',function(){
            var f_templateID=$(this).data('template');
            var f_type=$(this).data('id');
            $.post("infor_template.php",{
                templateID:f_templateID,
                type:f_type
            },function(data){
                $('#myModal').html(data);                
                $('#myModal').modal();
                updatetemplate();
            });  
        });
    }
    function updatetemplate(){
        $('#tpl_update').unbind('click').bind('click',function(){
            var f_wlus="";
            var f_unittype="";
            var tpl_id=$(this).data('id');
            var f_name=$('#tpl_name').val();
            var f_dsc=$("#tpl_dsc").val();
            var f_css=$(".l_"+tpl_id).data('css');
            if($("#tpl_wlus").length>0){
                var f_wlus=$("#tpl_wlus").val();
                var f_unittype=$("#unittype").val();
            }
            $.post("infor_template.php",{
                    u_type:"full",
                    css:f_css,
                    id:tpl_id,
                    name:f_name,
                    dsc:f_dsc,
                    wlus:f_wlus,
                    unittype:f_unittype
                },function(data){
                    $(".success-coin").remove();
                    $(".l_"+tpl_id).html(data);
                    bottomclc();
                    buttonclc();
            });
        });
    }
</script>
</body>
</html>
